package com.duan.laugh.mp.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.duan.laugh.common.core.util.R;
import com.duan.laugh.common.security.util.UserUtils;
import com.duan.laugh.mp.api.pojo.entity.MpFansMsg;
import com.duan.laugh.mp.service.IMpFansMsgService;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


/**
 * 微信粉丝消息管理
 *
 * @author duanjw
 * @date 2019-03-27 20:45:27
 */
@RestController
@AllArgsConstructor
@RequestMapping("/wxfansmsg")
public class MpFansMsgController {

    private final IMpFansMsgService IMpFansMsgService;

    /**
     * 分页查询
     *
     * @param page      分页对象
     * @param wxFansMsg 消息
     * @return
     */
    @GetMapping("/page")
    public R getWxFansMsgPage(Page page, MpFansMsg wxFansMsg) {
        wxFansMsg.setTenantId(UserUtils.getUser().getTenantId());
        return R.ok(IMpFansMsgService.page(page, Wrappers.query(wxFansMsg)));
    }


    /**
     * 通过id查询消息
     *
     * @param id id
     * @return R
     */
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Integer id) {
        return R.ok(IMpFansMsgService.getById(id));
    }

    /**
     * 新增消息
     *
     * @param wxFansMsg 消息
     * @return R
     */
    @PostMapping
    @PreAuthorize("@pms.hasPermission('mp_wxfansmsg_add')")
    public R save(@RequestBody MpFansMsg wxFansMsg) {
        wxFansMsg.setTenantId(UserUtils.getUser().getTenantId());
        return R.ok(IMpFansMsgService.save(wxFansMsg));
    }

    /**
     * 修改消息
     *
     * @param wxFansMsg 消息
     * @return R
     */
    @PutMapping
    @PreAuthorize("@pms.hasPermission('mp_wxfansmsg_edit')")
    public R updateById(@RequestBody MpFansMsg wxFansMsg) {
        wxFansMsg.setTenantId(UserUtils.getUser().getTenantId());
        return R.ok(IMpFansMsgService.updateById(wxFansMsg));
    }

    /**
     * 通过id删除消息
     *
     * @param id id
     * @return R
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('mp_wxfansmsg_del')")
    public R removeById(@PathVariable Integer id) {
        return R.ok(IMpFansMsgService.removeById(id));
    }

}
